
/*代码背景*/
UE.registerUI('btn_code', function(editor, uiName) {
    //创建一个button
    var btn = new UE.ui.Button({
        //按钮的名字
        name: "btn_code",
        //提示
        title: "代码",
        //添加额外样式，指定icon图标，这里默认使用一个重复的icon
        cssRules: 'background-position: -827px 0px;',
        //点击时执行的命令
        onclick: function() {
        	var range = editor.selection.getRange();
	        range.select();
	        var txt = editor.selection.getText();
	        txt = '<code>' + txt + '</code>';
	        editor.execCommand('insertHtml', txt);
        }
    });
    //当点到编辑内容上时，按钮要做的状态反射
    editor.addListener('selectionchange', function() {
        var state = editor.queryCommandState(uiName);
        if (state == -1) {
            btn.setDisabled(true);
            btn.setChecked(false);
        } else {
            btn.setDisabled(false);
            btn.setChecked(state);
        }
    });
    //因为你是添加button,所以需要返回这个button
    return btn;
});

/*标记*/
UE.registerUI('btn_mark', function(editor, uiName) {
    var btn = new UE.ui.Button({
        name: "btn_mark",
        title: "标记",
        cssRules: 'background-position: -967px 0px;',
        onclick: function() {
        	var range = editor.selection.getRange();
	        range.select();
	        var txt = editor.selection.getText();
	        txt = '<mark>' + txt + '</mark>';
	        editor.execCommand('insertHtml', txt);
        }
    });
    return btn;
});

/*键盘*/
UE.registerUI('btn_kbd', function(editor, uiName) {
    var btn = new UE.ui.Button({
        name: "btn_kbd",
        title: "键盘文字",
        cssRules: 'background-position: -947px 1px;',
        onclick: function() {
        	var range = editor.selection.getRange();
	        range.select();
	        var txt = editor.selection.getText();
	        txt = '<kbd>' + txt + '</kbd>';
	        editor.execCommand('insertHtml', txt);
        }
    });
    return btn;
});

/*折叠板*/
UE.registerUI('btn_collapse', function(editor, uiName) {
    var btn = new UE.ui.Button({
        name: "btn_collapse",
        title: "折叠板",
        cssRules: 'background-position: -847px 0px;',
        onclick: function() {
        	var timestamp=new Date().getTime();
            var collapseId = 'collapseId-' + timestamp;
        	var range = editor.selection.getRange();
	        range.select();
	        var txt = editor.selection.getText();
	        if(txt == ""){
	        	txt = "这里替换成你要显示或隐藏的内容"
	        }
	        txt = '<a class="collapse-style" data-toggle="collapse" href="#'+ collapseId +'" aria-expanded="true" aria-controls="'+ collapseId +'">展开或折叠代码<i class="fa fa-plus-circle pull-right mt-2"></i></a><div class="panel-collapse collapse in" id="'+ collapseId +'"><div class="card card-block mb-2">' + txt + '</div></div>';
	        editor.execCommand('insertHtml', txt);
        }
    });
    return btn;
});

/*进度条*/
UE.registerUI('btn_striped', function(editor, uiName) {
    var btn = new UE.ui.Button({
        name: "btn_striped",
        title: "进度条",
        cssRules: 'background-position: -847px -19px;',
        onclick: function() {
        	var range = editor.selection.getRange();
	        range.select();
	        var txt = editor.selection.getText();
	        txt = '<div class="progress"><div class="progress-bar progress-bar-striped" role="progressbar" style="width:' + txt + '%">' + txt + '%</div></div>';
	        editor.execCommand('insertHtml', txt);
        }
    });
    return btn;
});

/*下载按钮*/
UE.registerUI('btn_download', function(editor, uiName) {
    var btn = new UE.ui.Button({
        name: "btn_download",
        title: "本地下载",
        cssRules: 'background-position: -827px -19px;',
        onclick: function() {
        	var range = editor.selection.getRange();
	        range.select();
	        var txt = editor.selection.getText();
	        txt = '<div class="mt-2 mb-2 text-center"><a class="downbtn" href="' + txt + '" target="_blank" rel="nofollow"><i class="grace v3-download"></i> 本地下载</a></div>';
	        editor.execCommand('insertHtml', txt);
        }
    });
    return btn;
});

/*背景色框*/
UE.registerUI('btn_mark_warning btn_mark_danger btn_mark_info btn_mark_success', function(editor, uiName) {
	var css_bg = "",uiTitle='';
	switch(uiName){
		case 'btn_mark_warning':
		css_bg = "background-position: -927px 0px;";
		uiTitle = "警告";
		break;
		case 'btn_mark_success':
		css_bg = "background-position: -907px 0px;";
		uiTitle = "成功";
		break;
		case 'btn_mark_info':
		css_bg = "background-position: -887px 0px;";
		uiTitle = "提示";
		break;
		case 'btn_mark_danger':
		css_bg = "background-position: -867px 0px;";
		uiTitle = "报警";
		break;
	}
    //创建一个button
    var btn = new UE.ui.Button({
        //按钮的名字
        name: uiName,
        //提示
        title: uiTitle,
        //添加额外样式，指定icon图标，这里默认使用一个重复的icon
        cssRules: css_bg,
        //点击时执行的命令
        onclick: function() {
        	var range = editor.selection.getRange();
	        range.select();
	        var txt = editor.selection.getText();
	        switch(uiName){
				case 'btn_mark_warning':
				txt = '<div class="alert alert-warning">' + txt + '</div>';
				break;
				case 'btn_mark_success':
				txt = '<div class="alert alert-success">' + txt + '</div>';
				break;
				case 'btn_mark_info':
				txt = '<div class="alert alert-info">' + txt + '</div>';
				break;
				case 'btn_mark_danger':
				txt = '<div class="alert alert-danger">' + txt + '</div>';
				break;
			}
	        editor.execCommand('insertHtml', txt);
        }
    });
    //当点到编辑内容上时，按钮要做的状态反射
    editor.addListener('selectionchange', function() {
        var state = editor.queryCommandState(uiName);
        if (state == -1) {
            btn.setDisabled(true);
            btn.setChecked(false);
        } else {
            btn.setDisabled(false);
            btn.setChecked(state);
        }
    });
    //因为你是添加button,所以需要返回这个button
    return btn;
});

/*哔哩哔哩视频*/
UE.registerUI('btn_bilibili', function(editor, uiName) {
    var btn = new UE.ui.Button({
        name: "btn_bilibili",
        title: "哔哩哔哩视频",
        cssRules: 'background-position: -867px -19px;',
        onclick: function() {
        	var range = editor.selection.getRange();
	        range.select();
	        var txt = editor.selection.getText();
	        txt = '[bilibili cid=""]' + txt + '[//bilibili]';
	        editor.execCommand('insertHtml', txt);
        }
    });
    return btn;
});

/*网易云音乐*/
UE.registerUI('btn_music', function(editor, uiName) {
    var btn = new UE.ui.Button({
        name: "btn_music",
        title: "网易云音乐",
        cssRules: 'background-position: -887px -19px;',
        onclick: function() {
        	var range = editor.selection.getRange();
	        range.select();
	        var txt = editor.selection.getText();
	        txt = '[music]' + txt + '[/music]';
	        editor.execCommand('insertHtml', txt);
        }
    });
    return btn;
});

/*优酷视频*/
UE.registerUI('btn_youku', function(editor, uiName) {
    var btn = new UE.ui.Button({
        name: "btn_youku",
        title: "优酷视频",
        cssRules: 'background-position: -907px -19px;',
        onclick: function() {
        	var range = editor.selection.getRange();
	        range.select();
	        var txt = editor.selection.getText();
	        txt = '[youku]' + txt + '[/youku]';
	        editor.execCommand('insertHtml', txt);
        }
    });
    return btn;
});

/*腾讯视频*/
UE.registerUI('btn_vqq', function(editor, uiName) {
    var btn = new UE.ui.Button({
        name: "btn_vqq",
        title: "腾讯视频",
        cssRules: 'background-position: -927px -19px;',
        onclick: function() {
        	var range = editor.selection.getRange();
	        range.select();
	        var txt = editor.selection.getText();
	        txt = '[vqq auto="0"]' + txt + '[/vqq]';
	        editor.execCommand('insertHtml', txt);
        }
    });
    return btn;
});

/*YouTube*/
UE.registerUI('btn_YouTube', function(editor, uiName) {
    var btn = new UE.ui.Button({
        name: "btn_YouTube",
        title: "YouTube",
        cssRules: 'background-position: -947px -19px;',
        onclick: function() {
        	var range = editor.selection.getRange();
	        range.select();
	        var txt = editor.selection.getText();
	        txt = '[youtube]' + txt + '[/youtube]';
	        editor.execCommand('insertHtml', txt);
        }
    });
    return btn;
});

/*引用*/
UE.registerUI('btn_Reference', function(editor, uiName) {
    var btn = new UE.ui.Button({
        name: "btn_Reference",
        title: "文章引用",
        cssRules: 'background-position: -967px -19px;',
        onclick: function() {
        	var range = editor.selection.getRange();
	        range.select();
	        var txt = editor.selection.getText();
	        txt = '<fieldset class="layui-elem-field"><legend>引用原文</legend></fieldset>';
	        editor.execCommand('insertHtml', txt);
        }
    });
    return btn;
});

